package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.common.Constants;
import com.bdqn.common.PageSupport;
import com.bdqn.pojo.StorageRecord;
import com.bdqn.pojo.Supplier;
import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.StorageRecordService;
import com.bdqn.service.SupplierService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/storageRecord")
public class StorageRecordController {
    @Resource
    private StorageRecordService storageRecordService;

    @Resource
    private SupplierService supplierService;

    @GetMapping("/list")
     public String list(Model model, String queryGoodsName, Long querySupplierId, Long queryPayStatus, @RequestParam(defaultValue = "1") Integer pageIndex) {
        StorageRecord storageRecord = new StorageRecord();
        storageRecord.setGoodsName(queryGoodsName);
        storageRecord.setSupplierId(querySupplierId);
        storageRecord.setPayStatus(queryPayStatus);
        System.out.println(querySupplierId + "--------------------------------------------");

        PageSupport page = new PageSupport();
        page.setCurrentPageNo(pageIndex);
        page.setTotalCount(storageRecordService.count(storageRecord));
        List<StorageRecord> pageList = storageRecordService.getPageList(storageRecord, page);
        List<Supplier> supName = supplierService.getSupName();//供货商信息

        //数据回显
        model.addAttribute("queryGoodsName", queryGoodsName);
        model.addAttribute("querySupplierId", querySupplierId);
        model.addAttribute("queryPayStatus", queryPayStatus);
        model.addAttribute("pageIndex", pageIndex);

        model.addAttribute("page", page);
        model.addAttribute("storageRecordList", pageList);
        model.addAttribute("supplierList", supName);
        return "storageRecord/list";
    }

    @GetMapping("/view/{storageRecordId}")
    public String view(Model model, @PathVariable("storageRecordId") Integer storageRecordId) {
        StorageRecord storageRecord = storageRecordService.queryId(storageRecordId);
        model.addAttribute("storageRecord", storageRecord);
        return "storageRecord/view";
    }

    @GetMapping("/toUpdate/{storageRecordId}")
    public String toUpdate(Model model, @PathVariable("storageRecordId") Integer storageRecordId) {
        StorageRecord storageRecord = storageRecordService.queryId(storageRecordId);
        model.addAttribute("storageRecord", storageRecord);
        return "storageRecord/update";
    }

    @PostMapping("/update")
    public String update(HttpSession session, StorageRecord storageRecord) {
        SysUser userSession = (SysUser) session.getAttribute(Constants.USER_SESSION);
        storageRecord.setUpdatedUserId(userSession.getId());
        boolean update = storageRecordService.update(storageRecord);
        System.out.println(update);
        return "redirect:/storageRecord/list";
    }
    @GetMapping("/toAdd")
    public String toAdd(){
        return "storageRecord/add";
    }


    @PostMapping("/add")
    public String doAdd(HttpSession session, StorageRecord storageRecord) {
        storageRecord.setCreatedUserId(((SysUser) session.getAttribute(Constants.USER_SESSION)).getId());
        boolean flag = storageRecordService.add(storageRecord);
        return "redirect:/storageRecord/list";
    }

    /**
     * AJAX根据Id删除
     *
     * @param storageRecordId
     * @return
     */
    @ResponseBody
    @DeleteMapping("/del/{storageRecordId}")
    public String delete(@PathVariable("storageRecordId") Integer storageRecordId){
        Map<String,Object> map = new HashMap<>();
        StorageRecord storageRecord = storageRecordService.queryId(storageRecordId);
        boolean delete = storageRecordService.delete(storageRecordId);
        if (storageRecord == null){
            map.put("delResult","notexist");
            return JSON.toJSONString(map);
        }
        map.put("delResult",delete ? "true":"false");
        return JSON.toJSONString(map);
    }
}
